from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from ..serializers.sku_serializers import *
from ..paginations import MyPage

class SPUSpecView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class =SPUSpecModelSerializer

    def get_queryset(self):

        # 如何获取路径中的参数呢？这又不是视图中的函数？
        # 如果是命名分组使用： self.kwargs,  如果不是命名分组提取的参数，则使用self.args
        spu_id = self.kwargs.get("pk")
        return self.queryset.filter(spu_id=spu_id)

class SKUGoodsView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(name__contains=keyword)
        return self.queryset.all()


class SKUCategoryView(ListAPIView):
    queryset = GoodsCategory.objects.all()
    serializer_class = SKUCategorySimpleSerializer

    def get_queryset(self):
        # 过滤三级分类
        return self.queryset.filter(parent_id__gte=37)


class SPUSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer